package com.digitalchina.lxnsy.activity.old;

import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.AgrListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.xml.UserXML;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：公共URL 传入map
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/1/22 11:48
 * 修改备注：
 * ================================================
 */
public class NewAgrH5Activity extends BaseActivity {

    @Bind(R.id.web_view)
    WebView webView;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_praise)
    ImageView ivPraise;
    @Bind(R.id.tv_praise)
    TextView tvPraise;
    @Bind(R.id.layout_praise)
    LinearLayout layoutPraise;

    private Map<String, Object> map = new HashMap<>();
    private String title = "";
    private String url = "";
    private String shareUrl = "";
    private String shareImgUrl = "";
    private String id = "";
    private boolean isPraise = false;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_new_agr_h5);
        setTitle("新农艺详情");
        setRightText("分享");
    }

    @Override
    public void initMember() {


        AgrListVO vo = (AgrListVO) getIntent().getSerializableExtra("AgrListVO");

        if (vo == null)
            return;


        id = vo.getId();
        url = URL.URL_NEW_AGR_DETAIL_H5 + vo.getId() + "&userAccId=" + UserXML.getUserId(this);
        shareUrl = URL.URL_NEW_AGR_DETAIL_SHARE_H5 + vo.getId() + "&userAccId=" + UserXML.getUserId(this);
        shareImgUrl = JointImageUrl.smallImageUrl(vo.getImgId());
        title = vo.getNewAgriName();
        tvTitle.setText(title);
        tvPraise.setText(vo.getPraiseNum() + "赞同");

        BusinessAccount.appBrowse(this, id, "14", title, url, mHandler);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setUseWideViewPort(true); // 关键点
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.getSettings().setUseWideViewPort(true);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {

            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }

            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null) return false;

                //处理http和https开头的url
                if (url.startsWith("http://") || url.startsWith("https://"))
                    webView.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl(url);
    }

    // 覆盖onKeydown 添加处理WebView 界面内返回事件处理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void addListener() {
        layoutPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isPraise)
                    showToast("您已经赞过啦！");
                else
                    BusinessAccount.appPraise(NewAgrH5Activity.this, id, "14", title, url, mHandler);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PRAISE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PRAISE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        String s = FastJsonUtil.getString(object.toString(), "totalNum");
        tvPraise.setText(s + "赞同");
        ivPraise.setImageResource(R.drawable.icon_praise);
        isPraise = true;
    }

    @Override
    public void rightOnClick() {
       }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
        webView.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        webView.resumeTimers();
        webView.onResume();
    }


    @Override
    protected void onDestroy() {
        webView.destroy();
        webView = null;
        super.onDestroy();
    }
}
